<template>
  <div class="home">
    <!-- 头部 -->
    <div class="profile_title">
      <img
        src="http://124.223.73.204:8080/img/profile/bg.png"
        alt="背景图"
        class="bg"
      />
      <div class="profile_info">
        <!-- 头像 -->
        <div class="myIcon">
          <img
          v-if="token"
            :src="url+userInfo.avatar"
            alt="icon"
            class="avatar"
          />
          <img
          v-else
            src="http://124.223.73.204:8080/img/profile/avatar.png"
            alt="icon"
            class="avatar"
          />
        </div>
        <div class="user" v-if="token">
          <div class="name">{{userInfo.nickname}}</div>
          <div class="auth">
            <span @click="exit">退出</span>
          </div>
          <div class="edit">
            编辑个人资料
            <span class="arrow">
              <van-icon name="arrow"></van-icon>
            </span>
          </div>
        </div>
        <div class="user" v-else>
          <div class="name">游客</div>
          <div class="edit">
            <van-button type="primary" @click="$router.push('/login')"><span>去登录</span></van-button>
          </div>
        </div>
      </div>
    </div>
    <!-- 网格 -->
    <div class="gird">
      <van-grid :border="false" :column-num="3">
        <!-- <van-grid-item
          v-for="value in 6"
          :key="value"
          icon="photo-o"
          text="文字"
        /> -->
        <van-grid-item icon="star-o" text="我的收藏"></van-grid-item>
        <van-grid-item icon="wap-home-o" text="我的出租"></van-grid-item>
        <van-grid-item icon="clock-o" text="看房记录"></van-grid-item>
        <van-grid-item icon="ecard-pay" text="成为房主"></van-grid-item>
        <van-grid-item icon="contact" text="个人资料"></van-grid-item>
        <van-grid-item icon="service-o" text="联系我们"></van-grid-item>
      </van-grid>
    </div>
    <!-- 底部 -->
    <div class="footer">
      <img src="http://124.223.73.204:8080/img/profile/join.png" alt="" />
    </div>
  </div>
</template>

<script>
import { Dialog } from 'vant'
import { userApi } from '@/api/user'
import { getItem, removeItem } from '@/utils/storage'
export default {
  name: 'MyPage',
  data () {
    return {
      userInfo: [],
      token: getItem(),
      url: 'http://www.xiaoshuaipeng.com:8080'
    }
  },
  async created () {
    console.log(this.token)
    const res = await userApi()
    console.log(res)
    this.userInfo = res.data.body
  },
  methods: {
    exit () {
      Dialog.confirm({
        title: '提示',
        message: '是否确认退出？'
      })
        .then(() => {
          // 确认
          // 清除token
          removeItem()
          // this.$router.push('/login')
          this.$toast.success('退出成功！')
          this.$router.go(0) // 强制刷新页面
        })
        .catch(() => {
          // on cancel
        })
    }
  }
}
</script>

<style scoped lang="less">
.home {
  background-color: #fff;
}
.profile_title {
  min-height: 300px;
  margin-bottom: 12px;
  position: relative;
  background-color: #fff;
  .bg {
    width: 100%;
  }
  .profile_info {
    position: absolute;
    width: 85%;
    height: 55%;
    bottom: 0;
    left: 50%;
    transform: translateX(-50%);
    box-shadow: 0px 0px 10px 3px #ddd;
    margin: 50px auto 0;
    padding: 0 20px;
    background-color: #fff;
    text-align: center;
    font-size: 13px;
    // 头像
    .myIcon {
      position: relative;
      transform: translateY(-50%);
      border-radius: 50%;
      width: 70px;
      height: 70px;
      border: 5px solid #f5f5f5;
      display: inline-block;
      box-shadow: 0px 2px 2px #bdbdbd;
      .avatar {
        width: 100%;
        border-radius: 50%;
      }
    }
    .user {
      padding-top: 15px;
      .name {
        margin-top: -30px;
        margin-bottom: 10px;
      }
      .auth {
        margin-bottom: 30px;
        span {
          padding: 2px 15px;
          background-color: #21b97a;
          color: #fff;
          font-size: 12px;
          border-radius: 30px;
        }
      }
    }
    // 按钮
    .edit {
      color: #999;
      font-size: 12px;
      margin-top: 20px;
      .van-button {
        height: 30px;
        font-size: 13px;
        line-height: 30px;
        padding: 0 15px;
      }
    }
  }
}
// 底部
.footer {
  margin-top: 10px;
  text-align: center;
  img {
    width: 92%;
  }
}
</style>
